package com.wifipositioner.activity;

import java.io.File;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.wifipositioner.Globals;
import com.wifipositioner.R;
import com.wifipositioner.widget.FileNameChooserPopup;
import com.wifipositioner.wifi.WifiProvider;

public class FileChooserActivity extends ListActivity{
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getListView().setLongClickable(true);
		registerHandlers();
	}
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		if(hasFocus){
			File sdCardRoot = Environment.getExternalStorageDirectory();
			File myDir = new File(sdCardRoot, Globals.mapPath);
			String[] fileList = myDir.list();
			if(fileList == null){
				fileList = new String[]{};
			}
			this.setListAdapter(new ArrayAdapter<String>(this,
					android.R.layout.simple_list_item_1, fileList));
		}
		super.onWindowFocusChanged(hasFocus);
	}
	@Override
    public boolean onCreateOptionsMenu(Menu menu){
    	menu.add(Menu.NONE,0,0,"Create new");
    	return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
	    switch (item.getItemId()) {
	       case 0: { 
	    	   PopupWindow popupWindow;
	    	   //We need to get the instance of the LayoutInflater, use the context of this activity
	           LayoutInflater inflater = (LayoutInflater) this
	                   .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	           //Inflate the view from a predefined XML layout
	           View layout = inflater.inflate(R.layout.filenamepopup,
	                   (ViewGroup) findViewById(R.id.filenamepopup_root));
	           // create a 300px width and 470px height PopupWindow
	           popupWindow = new FileNameChooserPopup(layout, 300, 600, true);
	           // display the popup in the center
	           popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
	    	   break;
	       }
	       case 1: {
	    	   WifiProvider.get().scan();
    	       break;
	       }		
	    }
		return false;
    }
	@Override
	protected void onListItemClick(ListView l, final View v, int position, long id) {
		Intent newIntent = new Intent(FileChooserActivity.this, PositioningActivity.class);
		newIntent.putExtra(Globals.referenceFileNameExtra, ((TextView)v).getText());
		startActivity(newIntent);
		super.onListItemClick(l, v, position, id);
	}
	public void newReferenceMapCreated(String fileName){
		Intent newIntent = new Intent(FileChooserActivity.this, 
				MeasureActivity.class);
		newIntent.putExtra(Globals.referenceFileNameExtra, fileName);
		startActivity(newIntent);
	}

	private void registerHandlers(){
		getListView().setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, final View view,
					int position, long id) {
				final AlertDialog alertDialog = new AlertDialog.Builder(FileChooserActivity.this).create();
				alertDialog.setTitle("Delete file...");
				alertDialog.setMessage("Are you sure?");
				alertDialog.setButton("Cancel", new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						alertDialog.cancel();
						
					}
				});
				alertDialog.setButton2("Ok", new OnClickListener() {
							
					@Override
					public void onClick(DialogInterface dialog, int which) {
						File sdCardRoot = Environment.getExternalStorageDirectory();
						File file = new File(sdCardRoot, Globals.mapPath + ((TextView)view).getText().toString());
						file.delete();
						FileChooserActivity.this.getListView().forceLayout();
						
					}
				});
				alertDialog.setIcon(R.drawable.icon);
				alertDialog.show();
				return true;
			}
		});
	}


}
